package com.ruoyi.project.wenyanwen.data.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.wenyanwen.article.domain.Article;
import com.ruoyi.project.wenyanwen.article.service.IArticleService;
import com.ruoyi.project.wenyanwen.data.domain.UserData;
import com.ruoyi.project.wenyanwen.data.domain.UserFeature;
import com.ruoyi.project.wenyanwen.data.mapper.UserDataMapper;
import com.ruoyi.project.wenyanwen.data.service.IUserDataService;
import com.ruoyi.project.wenyanwen.tag.domain.ArticleTag;
import com.ruoyi.project.wenyanwen.tag.service.IArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户特征Service业务层处理
 *
 * @author shixiangwen
 * @date 2020-08-12
 */
@Service
@DataSource(value = DataSourceType.KKWENYANWEN)
public class UserDataServiceImpl implements IUserDataService {
    @Autowired
    private UserDataMapper userDataMapper;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private IArticleTagService articleTagService;

    /**
     * 查询用户特征
     *
     * @param uuid
     * @return 用户特征
     */
    @Override
    public UserData selectUserDataByUuid(String uuid) {
        return userDataMapper.selectUserDataByUuid(uuid);
    }

    /**
     * 查询用户特征列表
     *
     * @param userData 用户特征
     * @return 用户特征
     */
    @Override
    public List<UserData> selectUserDataList(UserData userData) {
        return userDataMapper.selectUserDataList(userData);
    }

    /**
     * 新增用户特征
     *
     * @param userData 用户特征
     * @return 结果
     */
    @Override
    public int insertUserData(UserData userData) {
        return userDataMapper.insertUserData(userData);
    }

    /**
     * 修改用户特征
     *
     * @param userData 用户特征
     * @return 结果
     */
    @Override
    public int updateUserData(UserData userData) {
        return userDataMapper.updateUserData(userData);
    }

    /**
     * 删除用户特征对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserDataByIds(String ids) {
        return userDataMapper.deleteUserDataByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户特征信息
     *
     * @param id 用户特征ID
     * @return 结果
     */
    @Override
    public int deleteUserDataById(Long id) {
        return userDataMapper.deleteUserDataById(id);
    }

    @Override
    public String formatFeature(String data) {
        UserFeature uf = JsonUtil.toObject(data, UserFeature.class);
        Map<String, Object> result = new HashMap<>();

        formatField(uf.getWatchTime(), result, "浏览文章记录", true, false);
        formatField(uf.getWatchTagTime(), result, "浏览标签记录", true, true);
        formatField(uf.getReciteTime(), result, "背诵文章记录", true, false);
        formatField(uf.getReciteTagTime(), result, "背诵标签记录", true, true);

        formatField(uf.getWatchCount(), result, "浏览文章次数", false, false);
        formatField(uf.getWatchTagCount(), result, "浏览标签次数", false, true);
        formatField(uf.getReciteCount(), result, "背诵文章次数", false, false);
        formatField(uf.getReciteTagCount(), result, "背诵标签次数", false, true);

        return JsonUtil.toJson(result);
    }

    private void formatField(Map<Integer, Long> source, Map<String, Object> result, String fieldDesc, boolean formatValue2Date, boolean isTag) {
        Map<String, Object> fieldFormat = new HashMap<>();
        for (Map.Entry<Integer, Long> entry : source.entrySet()) {
            String name = getName(isTag, entry.getKey());
            if (StringUtils.isEmpty(name)) {
                continue;
            }
            if (formatValue2Date) {
                fieldFormat.put(name, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date(entry.getValue())));
            } else {
                fieldFormat.put(name, entry.getValue());
            }
        }
        result.put(fieldDesc, fieldFormat);
    }

    private String getName(boolean isTag, long id) {
        if (isTag) {
            ArticleTag articleTag = articleTagService.selectArticleTagById(id);
            if (articleTag != null) {
                return articleTag.getName();
            } else {
                return "标签[" + id + "]";
            }
        } else {
            Article article = articleService.selectArticleById(id);
            if (article != null) {
                return article.getName();
            } else {
                return "文章[" + id + "]";
            }
        }
    }
}
